package com.mnemonic.views;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;

import com.mnemonic.support.swing.Dialogs;
import com.mnemonic.support.swing.Window;

/**
 * 主界面
 * 
 * @author lifeng
 * @date 2021/05/12
 */
public class MainPage extends Window {

    private MainPanel main;

    @Override
    public void initialize() {

        // 界面
        root = new JFrame();
        // Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(root.getGraphicsConfiguration());
        // Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        // double h = (dim.getHeight() - screenInsets.bottom) * 0.93;
        // double w = h * 1.66;

        // 此处设置大小
        double h = 500;
        double w = h * 1.66;

        // 居中显示
        root.setBounds(0, 0, (int)w, (int)h);
        root.setLocationRelativeTo(null);

        // 设置显示边框和title栏
        root.setUndecorated(false);
        root.setForeground(Color.WHITE);

        // 设置不能调整大小
        root.setResizable(false);

        // 这段是设置底色
        // root.getContentPane().setBackground(Color.decode("#3175af"));
        // root.getRootPane().setBorder(BorderFactory.createMatteBorder(3, 3, 3, 3, Color.decode("#3175af")));

        // 这段设置图标
        root.setIconImage(new ImageIcon(getClass().getResource("logo.png")).getImage());

        // 这段设置布局
        root.getContentPane().setLayout(new BorderLayout(0, 0));

        // 设置标题
        root.setTitle("扫雷");

        // 顶部菜单
        root.setJMenuBar(this.addMenu());

        // 中间区域
        main = this.createMain();
        root.add(main, BorderLayout.CENTER);

        super.initialize();
    }

    private JMenuBar addMenu() {
        JMenuBar menuBar = new JMenuBar();

        // 设置
        JMenu menu = new JMenu("对战");
        menu.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                main.showPlayPanel();
            }
        });
        menuBar.add(menu);

        // 对战
        menu = new JMenu("难度设置");
        menu.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                main.showLevelPanel();
            }
        });
        menuBar.add(menu);

        // 归档
        menu = new JMenu("归档");
        menu.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                main.showArchivePanel();
            }
        });
        menuBar.add(menu);

        // 玩家
        menu = new JMenu("玩家");
        menu.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                main.showPlayerPanel();
            }
        });
        menuBar.add(menu);
        return menuBar;
    }

    /**
     * 创建中间区域
     * 
     * @return
     */
    private MainPanel createMain() {
        return new MainPanel();
    }

    @Override
    public void show() {
        if (!this.closed) {
            root.setVisible(true);
            root.setExtendedState(JFrame.NORMAL);
        }
    }

    @Override
    public void close() {
        this.closed = true;
        root.dispose();
    }

    @Override
    protected void onClose(MouseEvent e) {
        int result = Dialogs.confirm("提醒", "确认关闭？");
        if (result == 0) {
            super.onClose(e);
        }
    }
}